package cn.kgc.dao.impl;

import cn.kgc.dao.SmbmsRoleDao;
import cn.kgc.entity.SmbmsRole;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Rock
 * @create 2020-08-24 9:47
 */
@Repository("SmbmsRoleDao")
public class SmbmsRoleDaoImpl implements SmbmsRoleDao {
    /**
     * 创建QUeryRunner对象执行sql语句
     * 执行查询:query()
     * 增删改:update()
     */
    @Autowired
    private QueryRunner queryRunner;

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<SmbmsRole> getAllRole() throws SQLException {
        String sql = "select * from smbms_role";
        return queryRunner.query(sql,new BeanListHandler<SmbmsRole>(SmbmsRole.class));
    }

    /**
     * 根据id查询角色
     *
     * @param id
     * @return
     */
    @Override
    public SmbmsRole getRoleById(Integer id) throws SQLException {
        String sql = "select * from smbms_role where id=?";
        List<SmbmsRole> roleList= queryRunner.query(sql,new BeanListHandler<SmbmsRole>(SmbmsRole.class),id);
        if(roleList.size()==0){
            return null;
        }else{
            return roleList.get(0);
        }
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @Override
    public Integer addRole(SmbmsRole role) throws SQLException {
        String sql = "insert into smbms_role(roleCode, roleName, createdBy, creationDate) VALUES(?,?,?,?) ";
        return queryRunner.update(sql,role.getRolecode(),role.getRolename(),role.getCreatedby(),role.getCreationdate());

    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @Override
    public Integer updateRole(SmbmsRole role) throws SQLException {
        String sql = "update smbms_role set roleName=? where id=?";
        return queryRunner.update(sql,role.getRolename(),role.getId());
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Override
    public Integer delRole(Integer id) throws SQLException {
        String sql = "delete from smbms_role where id=?";
        return queryRunner.update(sql,id);

    }
}
